{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "from sklearn.feature_extraction.text import TfidfVectorizer\n",
    "from sklearn.cluster import KMeans\n",
    "import numpy as np\n",
    "import pandas as pd"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [],
   "source": [
    "text = [\"It is a good place to travel\", \n",
    "            \"Football is a nice game\", \"Lets go for holidays and travel to Egypt\", \n",
    "            \"It is a goal, a great game.\", \"Enjoy your journey and fortget the rest\", \"The teams are ready for the same\" ]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [],
   "source": [
    "tfidf_vectorizer = TfidfVectorizer(stop_words='english')\n",
    "X = tfidf_vectorizer.fit_transform(text)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "KMeans(max_iter=10, n_clusters=2, n_init=2)"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "k = 2\n",
    "model = KMeans(n_clusters=k, init='k-means++', max_iter=10, n_init=2)\n",
    "model.fit(X)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [],
   "source": [
    "centroids = model.cluster_centers_.argsort()[:, ::-1]\n",
    "features = vectorizer.get_feature_names()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Cluster 0:\n",
      "travel\n",
      "ready\n",
      "teams\n",
      "good\n",
      "place\n",
      "lets\n",
      "holidays\n",
      "egypt\n",
      "journey\n",
      "rest\n",
      "Cluster 1:\n",
      "game\n",
      "great\n",
      "football\n",
      "nice\n",
      "goal\n",
      "travel\n",
      "enjoy\n",
      "fortget\n",
      "good\n",
      "holidays\n"
     ]
    }
   ],
   "source": [
    "for i in range(k):\n",
    "    print(\"Cluster %d:\" % i),\n",
    "    for ind in centroids[i, :10]:\n",
    "        print(\"%s\" % terms[ind])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
